﻿using System;
using System.Collections.Generic;
using System.Linq;
using UserManagement.Dal;
using UserManagement.DomainEntity.Exceptions;

namespace UserManagement.DomainEntity
{
    public class UserCircle
    {
        #region constructor

        public UserCircle()
        {
            UserAccounts = new List<UserAccount>();
        }

        #endregion constructor

        #region public properties

        public virtual int? Id { get; private set; }

        public virtual string Name { get; set; }

        public virtual string Comment { get; set; }

        public virtual UserAccount Owner { get; set; }

        public virtual IList<UserAccount> UserAccounts { get; private set; }

        #endregion public properties

        #region public method

        public virtual void AddUserAccount(UserAccount account)
        {
            UserAccounts.Add(account);
        }

        #endregion public method

        #region public static methods

        public static UserCircle AddCircle(IRepository repository, UserCircle circle)
        {
            if (repository == null)
            {
                throw new ArgumentNullException("repository");
            }

            string errorMsg;
            if (!IsValidNewUserCircle(repository, circle, out errorMsg))
            {
                throw new NewUserCircleInvalidException(errorMsg);
            }

            circle = repository.Add(circle);
            return circle;
        }

        public static UserCircle FindById(IRepository repository, int id)
        {
            return repository.FindEntityById<UserCircle>(id);
        }

        public static bool IsValidNewUserCircle(IRepository repository, UserCircle circle, out string errorMsg)
        {
            if (circle == null)
            {
                errorMsg = "Null UserCircle object";
                return false;
            }

            if (string.IsNullOrEmpty(circle.Name))
            {
                errorMsg = "Null or empty name found";
                return false;
            }

            if (circle.Owner == null)
            {
                errorMsg = string.Format("Null owner of the circle : {0}", circle.Name);
                return false;
            }

            // the owner of the circle should be valid
            if (!UserAccount.IsValidExistUserAccount(repository, circle.Owner, out errorMsg))
            {
                return false;
            }

            // check to see if user name already exists
            var uc = repository.FindEntities<UserCircle>(cir => cir.Name.Trim().Equals(circle.Name.Trim(), StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
            bool result;
            if (uc != null)
            {
                errorMsg = "Circle name already exists in system";
                result = false;
            }
            else
            {
                errorMsg = string.Empty;
                result = true;
            }

            return result;
        }

        #endregion public static methods
    }
}